
import java.io.*;
import java.util.Scanner;


public class Demo8 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("請輸入要掃描的的路徑");
        String rootDirpath=sc.nextLine();

        File  rootDir=new File(rootDirpath);
        if(!rootDir.isDirectory())//不存在目录
        {
            System.out.println("输入的目录有误!");
            return;
        }

        System.out.print("请输入要找出的文件名中的字符: ");
        String word = sc.next();


        scanDir(rootDir,word);

    }

    private static void scanDir(File rootDir, String word) {

        File[] files=rootDir.listFiles();
        if(files==null)
        {
            return;
        }
        for(File f:files)
        {
            System.out.println("当前搜索到："+f.getAbsolutePath());
            if(f.isFile())
            {
                String contine=ReadFile(f);
                if(contine.contains(word))
                {
                    System.out.println(f.getAbsolutePath()+"包含要查找的关键字！");
                }
            }
            else if(f.isDirectory())
            {
                scanDir(f,word);
            }
            else
            {
                continue;
            }
        }

    }

    private static String ReadFile(File f) {
        StringBuilder sb=new StringBuilder();
        try( Reader reader=new FileReader(f)) {
           while(true)
           {
               int c= reader.read();
               if(c==-1)
               {
                   break;
               }
               sb.append((char)c);
           }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


}
